package com.miracle9.game.dao;

import com.miracle9.game.entity.UserActivity;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class UserActivityServiceDao extends SqlOperating<UserActivity, Integer> {

    public UserActivity getUserActivityByUserId(Integer userId) {
        return (UserActivity) q_CreateSqlList("from UserActivity where userId = ? order by datetime desc", new Object[]{userId});
    }

    public Integer saveReturnInteger(UserActivity userActivity) {
        return (Integer) saveReturn(userActivity);
    }

    public void deleteByActivityId(int nActivityId) {
        q_CreateSql("delete from UserActivity where activityId=?", new Object[]{Integer.valueOf(nActivityId)}).executeUpdate();
    }

    public void updateStatus_0(int nActivityState) {
        q_CreateSql("update UserActivity set status=0", new Object[0]).executeUpdate();
    }

    public boolean updateStatusById(int nStatus, int nUserActivityId) {
        Query query = q_CreateSql("update UserActivity set status=? where id=? ", new Object[]{Integer.valueOf(nStatus), Integer.valueOf(nUserActivityId)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public List<UserActivity> getUserActivitysByActivityId(int nActivityId) {
        return q_CreateSql("from UserActivity where activityId = ?", new Object[]{Integer.valueOf(nActivityId)}).list();
    }

    public List<UserActivity> getUserActivitysByUserIdAndType(int nUserId, int nType) {
        List<UserActivity> list = q_CreateSqlList("from UserActivity where userId=? and type = ?", new Object[]{Integer.valueOf(nUserId), Integer.valueOf(nType)});
        if (list != null) {
            Collections.sort(list, new Comparator<UserActivity>() {
                public int compare(UserActivity o1, UserActivity o2) {
                    return o1.getCondition1() - o2.getCondition1();
                }
            });
        }
        return list;
    }

    public UserActivity getByUserIdAndActivityId(int nUserId, int nActivityId) {
        UserActivity userActivity = new UserActivity();
        userActivity = (UserActivity) q_CreateSqlFilter("from UserActivity where userId=? and activityId=? ", new Object[]{Integer.valueOf(nUserId), Integer.valueOf(nActivityId)});
        return userActivity;
    }

    public UserActivity getByUserIdAndTypeAndNCondition1(int nUserId, int nType, int nCondition1) {
        UserActivity userActivity = new UserActivity();
        userActivity = (UserActivity) q_CreateSqlFilter("from UserActivity where userId=? and type = ? and condition1= ? ", new Object[]{Integer.valueOf(nUserId), Integer.valueOf(nType), Integer.valueOf(nCondition1)});
        return userActivity;
    }

    public boolean updateBonusCountById(int nUserActivityId, int nBonusCount) {
        Query query = q_CreateSql("update UserActivity set bonus_count=? where id=? ", new Object[]{Integer.valueOf(nBonusCount), Integer.valueOf(nUserActivityId)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public boolean updateBonusCount_0() {
        Query query = q_CreateSql("update UserActivity set bonus_count=0  ", new Object[0]);
        int count = query.executeUpdate();
        return count > 0;
    }

    public boolean updateCondition2(int nLevel, int nUserId, int nType) {
        Query query = q_CreateSql("update UserActivity set condition2=? where user_id=? and type=? ", new Object[]{Integer.valueOf(nLevel), Integer.valueOf(nUserId), Integer.valueOf(nType)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public List<UserActivity> getAllUserActivity(int nType) {
        return q_CreateSql("from UserActivity where  type = ?", new Object[]{Integer.valueOf(nType)}).list();
    }

    public boolean updateStatusByType(int nStatus) {
        Query query = q_CreateSql("update UserActivity set status=? where type=2 or type =4 or type=5", new Object[]{Integer.valueOf(nStatus)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public void updateDaySumYa_0ByType_2() {
        q_CreateSql("update UserActivity set day_sum_ya=0 where type=2", new Object[0]).executeUpdate();
    }

    public void updateSumInfo() {
        q_CreateSql("update UserActivity set daySumYa=0,payBeforeSumYa=0,payAfterSumYa=0,condition3=-1", new Object[0]).executeUpdate();
    }

}